﻿//***************************************************************************
// Project:   MDT Wizard Studio
// File:      Utility.cs
// Purpose:   Utility routines used by other classses and forms.
//
// Version:   2.0.0
//
// Change history:
// 2.0.0  mniehaus  10/11/2009  Created initial version.
//
// 3.0.2  mkoster   01/11/2013  Renamed to MDT Wizard Studio
//***************************************************************************

namespace MDTWizardStudio
{
    using System;
    using System.IO;
    using Microsoft.Win32;

    /// <summary>
    /// The Utility class contains various utility routines used by other classes and forms.
    /// </summary>
    class Utility
    {
        #region Instance variables

        private static Boolean initialized;
        private static String mdtInstallDir;

        #endregion Instance variables

        #region Constructor

        /// <summary>
        /// Constructor (not used as all methods are static)
        /// </summary>
        private Utility() {}

        #endregion Constructor

        #region Internal properties

        internal static String InstallDir
        {
            get
            {
                if (!initialized)
                {
                    // Retrieve the MDT installation directory (if MDT is installed) and save it in a
                    // static variable.
                    RegistryKey mdtKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Deployment 4");
                    if (mdtKey != null)
                    {
                        mdtInstallDir = mdtKey.GetValue("Install_Dir", "").ToString();
                    }
                    else
                    {
                        mdtInstallDir = String.Empty;
                    }
                    initialized = true;
                }
                return mdtInstallDir;
            }
        }

        #endregion Internal properties

        #region Internal methods

        /// <summary>
        /// The FindFile function is used to help locate the needed wizard files.  (This form
        /// expects to run on a machine with MDT installed.)
        /// </summary>
        /// <param name="definitionDirectory">The directory path containing the wizard definition file</param>
        /// <param name="fileName">The name of the file to find</param>
        /// <returns></returns>
        internal static String FindFile(String definitionDirectory, String fileName)
        {
            String mdtDistributionDir;

            // See if there is a deployment share
            if (File.Exists(Directory.GetParent(definitionDirectory).FullName + @"\Control\Version.xml"))
                mdtDistributionDir = Directory.GetParent(definitionDirectory).FullName;
            else
                mdtDistributionDir = String.Empty;

            // First look in the deployment share directory, if one exists
            if (mdtDistributionDir.Length != 0 && File.Exists(mdtDistributionDir + @"Scripts\" + fileName))
                return mdtDistributionDir + @"Scripts\" + fileName;

            // Then look in the installation directory, if one exists
            if (Utility.InstallDir.Length != 0 && File.Exists(Utility.InstallDir + @"Templates\Distribution\Scripts\" + fileName))
                return Utility.InstallDir + @"Templates\Distribution\Scripts\" + fileName;

            // Finally see if it is present in the same directory as the current XML file
            if (File.Exists(definitionDirectory + @"\" + fileName))
                return definitionDirectory + @"\" + fileName;

            // Still not found, return blank
            return "";
        }

        #endregion Internal methods

    }
}
